$(document).ready(function(){

    $("tbody td").buttonset();
    var columnid = $.query.get("columnid");
    if (columnid != "") {
        $("#columnid").val(columnid);
    }
    var __selectJump = function(){
        location.href = "ArticleList?columnid=" + $(this).val();
    }
    $("#columnid").change(__selectJump);
    var resetEvenColor = function(){
        $('tbody tr').removeClass("alt-row");
        $('tbody tr:even').delay(100).addClass("alt-row");
    };
    
    var __deleteHandler = function(){
        var tmp = $(this);
        $.post("/action/master/DeleteArticle", {
            Action: "post",
            id: tmp.attr("href")
        }, function(data, textStatus){
            if (!data.success) {
                $(".loadingbox").empty();
                $(".loadingbox").append(loadingTpl, {
                    icoType: 'fail',
                    loadIcon: '',
                    msgHtml: data.message
                });
				$(".loadingbox").fadeIn(50);
                $(".loadingbox").delay(3000).fadeOut(600);
            }
            else {
                $(".loadingbox").empty();
                $(".loadingbox").append(loadingTpl, {
                    icoType: "succ",
                    loadIcon: "",
                    msgHtml: data.message
                });
                $(".loadingbox").fadeIn(50);
                $(".loadingbox").delay(2000).fadeOut(600);
                tmp.parents('tr').effect("highlight", {
                    color: "#F00"
                }, 800, function(){
                    $(this).remove();
                    resetEvenColor();
                });
            }
        }, "json");
        return false;
    }
    $(".deleteBtn").click(__deleteHandler).easyconfirm({
        locale: {
            title: '操作确认',
            text: '您确定要删除这篇文章么？',
            button: ['取消', '是的，确定删除']
        }
    });
    
    
    var __recommendedHandler = function(e){
        var tmp = $(this);
        var action = "/action/master/unArticleTopic";
        if (tmp.is(':checked')) {
            action = "/action/master/setArticleTopic";
        }
        $.post(action, {
            Action: "post",
            id: tmp.val()
        }, function(data, textStatus){
            if (!data.success) {
                $(".loadingbox").empty();
                $(".loadingbox").append(loadingTpl, {
                    icoType: 'fail',
                    loadIcon: '',
                    msgHtml: data.message
                });
				$(".loading").fadeIn(50);
                $(".loadingbox").delay(3000).fadeOut(600);
            }
            else {
                $(".loadingbox").empty();
                $(".loadingbox").append(loadingTpl, {
                    icoType: "succ",
                    loadIcon: "",
                    msgHtml: data.message
                });
                $(".loadingbox").fadeIn(50);
                $(".loadingbox").delay(2000).fadeOut(600);
            }
        }, "json");
    }
    $("[type='checkbox']").click(__recommendedHandler);
});
